//@author A0085897A

import java.io.File;
import java.io.IOException;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class FileExport {

	// ATTRIBUTES
	private String _filename;
	private Transactions _transactions;
	private WritableWorkbook _workbook;

	// CONSTANTS
	protected final static String DEFAULT_FILENAME = "report.xls";

	// CONSTRUCTOR
	public FileExport(Transactions transactions, String file) {
		try {
			if (file.equals("")) {
				this._filename = DEFAULT_FILENAME;
			} else if (file.endsWith(".xls") || file.endsWith(".xlsx")) {
				this._filename = file;
			} else {
				this._filename = file + ".xls";
			}
			this._transactions = transactions;
			this._workbook = Workbook.createWorkbook(new File(this.getFilename()));
			this.getWorkbook().createSheet("Simplex", 0);
			this.writeFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// METHODS
	// Getters
	public String getFilename() {
		return this._filename;
	}
	public List<Transaction> getTransactions() {
		return this._transactions.getTransactions();
	}
	public WritableWorkbook getWorkbook() {
		return this._workbook;
	}
	public WritableSheet getSheet() {
		return this.getWorkbook().getSheet(0);
	}
	// Write transactions to the Excel file
	public void writeFile() {
		try {
			String[] headers = new String[]{"#", "Name", "Value", "Date", "Category", "Payment Method", "Remarks", "Status"};
			for (int i=0; i<headers.length; i++) {
				this.addCell(headers[i], 0, i);
			}
			for (int i=0; i<this.getTransactions().size(); i++) {
				Transaction transaction = this.getTransactions().get(i);
				double value = transaction.getValue();
				if (transaction.getType().equals(TransactionType.EXPENSE)) {
					value *= -1;
				}
				this.addCell(i+1, i+1, 0);
				this.addCell(transaction.getName(), i+1, 1);
				this.addCell(value, i+1, 2);
				this.addCell(transaction.getDateStr(), i+1, 3);
				this.addCell(transaction.getCategory(), i+1, 4);
				this.addCell(transaction.getPaymentMethod(), i+1, 5);
				this.addCell(transaction.getRemarks(), i+1, 6);
				this.addCell(transaction.getStatus().toString(), i+1, 7);
			}
			this.getWorkbook().write();
			this.getWorkbook().close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}
	// Add cell
	private void addCell(double num, int row, int col) {
		try {
			this.getSheet().addCell(new Number(col, row, num));
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} 
	}
	private void addCell(int num, int row, int col) {
		try {
			this.getSheet().addCell(new Number(col, row, num));
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} 
	}
	private void addCell(String str, int row, int col) {
		try {
			this.getSheet().addCell(new Label(col, row, str));
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}
	
}
